{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "671c768d-a514-4eef-af5d-05636a4f14ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "68871276-f907-45ae-b6da-d7f6bbce9f0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择图片\n",
    "target = 1\n",
    "data_name = ['0618', '0854', '1066'][target - 1]\n",
    "\n",
    "# 数据暂存\n",
    "features = np.empty((0, 27))\n",
    "labels = np.empty((0,))\n",
    "current_label = None  # 确保current_label在函数外部初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6b1bc5fd-1ec3-4fe5-9553-af39f4405071",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 鼠标回调函数\n",
    "def on_mouse(event, x, y, flags, param):\n",
    "    global features, labels, current_label, img\n",
    "\n",
    "    if event == cv2.EVENT_LBUTTONDOWN:\n",
    "        if 1 < x < img.shape[1] - 1 and 1 < y < img.shape[0] - 1:\n",
    "            feature_vector = np.zeros((27,))\n",
    "            index = 0\n",
    "            for i in range(-1, 2):\n",
    "                for j in range(-1, 2):\n",
    "                    nx, ny = x + i, y + j\n",
    "                    feature_vector[index:index + 3] = img[ny, nx]\n",
    "                    index += 3\n",
    "            features = np.vstack((features, feature_vector))\n",
    "            labels = np.append(labels, current_label)\n",
    "            print(f'Collected 3x3 BGR feature at position: {(x, y)} with label: {current_label}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fa1351d8-6289-461d-bd73-35d5080b2b09",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n",
      "QObject::moveToThread: Current thread (0x55cf6811fde0) is not the object's thread (0x55cf67989a30).\n",
      "Cannot move to target thread (0x55cf6811fde0)\n",
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collected 3x3 BGR feature at position: (270, 69) with label: 1\n",
      "Collected 3x3 BGR feature at position: (305, 70) with label: 1\n",
      "Collected 3x3 BGR feature at position: (337, 70) with label: 1\n",
      "Collected 3x3 BGR feature at position: (284, 107) with label: 1\n",
      "Collected 3x3 BGR feature at position: (215, 111) with label: 1\n",
      "Collected 3x3 BGR feature at position: (304, 112) with label: 1\n",
      "Collected 3x3 BGR feature at position: (304, 138) with label: 1\n",
      "Collected 3x3 BGR feature at position: (130, 143) with label: 1\n",
      "Collected 3x3 BGR feature at position: (99, 162) with label: 1\n",
      "Collected 3x3 BGR feature at position: (292, 171) with label: 1\n",
      "Collected 3x3 BGR feature at position: (404, 184) with label: 1\n",
      "Collected 3x3 BGR feature at position: (457, 217) with label: 1\n",
      "Collected 3x3 BGR feature at position: (328, 226) with label: 1\n",
      "Collected 3x3 BGR feature at position: (153, 224) with label: 1\n",
      "Collected 3x3 BGR feature at position: (46, 224) with label: 1\n",
      "Collected 3x3 BGR feature at position: (85, 176) with label: 1\n",
      "Collected 3x3 BGR feature at position: (173, 206) with label: 1\n",
      "Collected 3x3 BGR feature at position: (314, 189) with label: 1\n",
      "Collected 3x3 BGR feature at position: (268, 159) with label: 1\n",
      "Collected 3x3 BGR feature at position: (19, 29) with label: 2\n",
      "Collected 3x3 BGR feature at position: (39, 113) with label: 2\n",
      "Collected 3x3 BGR feature at position: (73, 85) with label: 2\n",
      "Collected 3x3 BGR feature at position: (87, 39) with label: 2\n",
      "Collected 3x3 BGR feature at position: (141, 79) with label: 2\n",
      "Collected 3x3 BGR feature at position: (246, 29) with label: 2\n",
      "Collected 3x3 BGR feature at position: (355, 9) with label: 2\n",
      "Collected 3x3 BGR feature at position: (437, 10) with label: 2\n",
      "Collected 3x3 BGR feature at position: (474, 34) with label: 2\n",
      "Collected 3x3 BGR feature at position: (457, 76) with label: 2\n",
      "Collected 3x3 BGR feature at position: (417, 90) with label: 2\n",
      "Collected 3x3 BGR feature at position: (462, 130) with label: 2\n",
      "Collected 3x3 BGR feature at position: (469, 90) with label: 2\n",
      "Collected 3x3 BGR feature at position: (156, 16) with label: 3\n",
      "Collected 3x3 BGR feature at position: (167, 19) with label: 3\n",
      "Collected 3x3 BGR feature at position: (172, 19) with label: 3\n",
      "Collected 3x3 BGR feature at position: (158, 17) with label: 3\n",
      "Collected 3x3 BGR feature at position: (156, 12) with label: 3\n"
     ]
    }
   ],
   "source": [
    "# 开启新窗口\n",
    "cv2.namedWindow('image')\n",
    "cv2.setMouseCallback(\"image\", on_mouse)\n",
    "\n",
    "# 加载图片\n",
    "img_path = f'./input_data/{data_name}.png'\n",
    "img = cv2.imread(img_path)\n",
    "\n",
    "while True:\n",
    "    cv2.imshow('image', img)\n",
    "    key = cv2.waitKey(1) & 0xFF\n",
    "\n",
    "    if key == ord('1'):\n",
    "        current_label = 1\n",
    "    elif key == ord('2'):\n",
    "        current_label = 2\n",
    "    elif key == ord('3'):\n",
    "        current_label = 3\n",
    "    elif key == 27:  # Esc key\n",
    "        break\n",
    "\n",
    "# 关闭窗口\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f02d6c3a-8820-4813-9965-6dac2533e2ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据保存到 CSV 文件的函数\n",
    "def save_to_csv(features, labels, filename):\n",
    "    # 检查文件是否存在\n",
    "    if os.path.exists(filename):\n",
    "        existing_data = pd.read_csv(filename)\n",
    "        new_data = pd.DataFrame(features, columns=[\n",
    "            f'B{i//3 + 1}_{i%3 + 1}' if (i%3) == 0 else \n",
    "            f'G{i//3 + 1}_{i%3 + 1}' if (i%3) == 1 else \n",
    "            f'R{i//3 + 1}_{i%3 + 1}' \n",
    "            for i in range(27)\n",
    "        ])\n",
    "        new_data['Label'] = labels\n",
    "        combined_data = pd.concat([existing_data, new_data], ignore_index=True)\n",
    "        combined_data.to_csv(filename, index=False)\n",
    "    else:\n",
    "        new_data = pd.DataFrame(features, columns=[\n",
    "            f'B{i//3 + 1}_{i%3 + 1}' if (i%3) == 0 else \n",
    "            f'G{i//3 + 1}_{i%3 + 1}' if (i%3) == 1 else \n",
    "            f'R{i//3 + 1}_{i%3 + 1}' \n",
    "            for i in range(27)\n",
    "        ])\n",
    "        new_data['Label'] = labels\n",
    "        new_data.to_csv(filename, index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7d4be6d2-5aa3-4883-892a-2585e3a37bd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存数据到 CSV 文件\n",
    "output_filename = f'./RGB_data/data_{data_name}_3x3.csv'\n",
    "save_to_csv(features, labels, output_filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54197348-36b4-43ed-bc87-590710c5b5d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
